//
//  TTStationsViewController.m
//  TrainTrack
//
//  Created by Jonathan Bunde-Pedersen on 04/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "TTStationsViewController.h"
#import "TrainTrackAppDelegate.h"
#import "Station.h"

@implementation TTStationsViewController

@synthesize navigationController, departuresController, stations, editBarButton;

// Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

	    self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
	    self.tableView.delegate = self;
	    self.tableView.dataSource = self;
	    self.tableView.rowHeight = 50;
	    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
	    self.tableView.sectionHeaderHeight = 0;
	    self.tableView.multipleTouchEnabled = YES;
	    self.tableView.delaysContentTouches = YES;
	    self.tableView.scrollEnabled = YES;
	    
	    self.departuresController = [[TTDepaturesViewController alloc] initWithStyle:UITableViewStyleGrouped];

	    
	    TrainTrackAppDelegate *appController = (TrainTrackAppDelegate *)[[UIApplication sharedApplication] delegate];
		
		self.stations = [appController.model fetchStations];
	    [self.tableView reloadData];	    
    }
    return self;
}


/*
// Implement loadView to create a view hierarchy programmatically.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view.
/*- (void)viewDidLoad {
    [super viewDidLoad];
}*/



// 
// TABLECONTROLLER
//

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.stations count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
	}
	
	// Set up the cell
	if([self.stations count] > indexPath.row)
		cell.text = [self.stations objectAtIndex:indexPath.row];
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	// Navigation logic -- create and push a new view controller
	
	// push new view onto navigationcontroller
	self.departuresController.station = [self.stations objectAtIndex:indexPath.row];
	[self.navigationController pushViewController:self.departuresController animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

//
//
//

/*
 - (void)viewWillAppear:(BOOL)animated {
}*/


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}


@end
